package com.answerz.module_reportissues.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.answerz.module_reportissues.BR
import com.answerz.module_reportissues.R
import com.answerz.module_reportissues.adapter.AttachImageAdapter
import com.answerz.module_reportissues.adapter.OrderStateAdapter
import com.answerz.module_reportissues.databinding.FragmentComplaintDetailBinding
import com.answerz.module_reportissues.view.FullyGridLayoutManager
import com.answerz.module_reportissues.viewmodel.ComplaintDetailViewModel
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper

@Route(path = AppConstants.Router.Complaint.F_COMPLAINT_DETAIL)
class ComplaintDetailFragment : BaseFragment<FragmentComplaintDetailBinding,ComplaintDetailViewModel>(){
    lateinit var oAdapter: AttachImageAdapter
    lateinit var sAdapter: OrderStateAdapter
    override fun initContentView(): Int {
        return R.layout.fragment_complaint_detail
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }
    override fun initData() {
        var no= arguments?.getString(AppConstants.BundleKey.WORK_ORDER_NO).toString()
        viewModel.tvTitle.set("投诉建议")
        viewModel.getData(no)
        oAdapter=AttachImageAdapter(this).apply {

        }
        sAdapter= OrderStateAdapter(this)
    }
    @SuppressLint("NotifyDataSetChanged")
    override fun initViewObservable() {
        binding.rvReportSubmit.apply {
            layoutManager= FullyGridLayoutManager(context,4, GridLayoutManager.VERTICAL, false)
            adapter=oAdapter
        }
        viewModel.attachImagesEvent.observe(this,{
            oAdapter.urls=it
            oAdapter.notifyDataSetChanged()
        })
        binding.rvOrderState.apply {
            layoutManager= LinearLayoutManager(context, LinearLayoutManager.VERTICAL,true)
            adapter=sAdapter
        }
        viewModel.orderStateFlowEvent.observe(this,{
            sAdapter.sl.clear()
            sAdapter.sl.addAll(it)
            sAdapter.notifyDataSetChanged()
        })
        binding.tvYezhuPhone.setOnClickListener {

            DialogHelper.showBottomListDialog(
                requireContext(),
                "联系方式",
                arrayOf(viewModel.reportPhone.get().toString())
            ) { _: Int, text: String? -> text?.let { it1 -> callPhone(it1) } }
        }
        binding.tvWuyePhone.setOnClickListener{

            DialogHelper.showBottomListDialog(
                requireContext(),
                "联系方式",
                arrayOf(viewModel.orderMaintainPhone.get().toString())
            ) { _: Int, text: String? -> text?.let { it1 -> callPhone(it1) } }
        }
        viewModel.onRefreshFinishEvent.observe(this,{
            binding.smartCommon.finishRefresh(it)
            if (!it){
                showErrorStatePage()
            }else{
                showSuccessStatePage()
            }
        })
//        binding.tvOrderContent.setOnClickListener{
//            DialogHelper.showBottomListDialog(
//                requireContext(),
//                "选择当前订单状态",
//                arrayOf("待接单","处理中","待支付","待审核","待评价","已评价")
//            ) { _: Int, text: String? -> text?.let { it1 -> viewModel.setOrderState(it1) } }
//        }
        viewModel.cancelOrderEvent.observe(this, {
            DialogHelper.showBaseDialog(requireContext(), "", "确定撤销该订单吗？") {
                viewModel.cancelComplaint(it)
            }
        })

        viewModel.deleteOrderEvent.observe(this, {
            DialogHelper.showBaseDialog(requireContext(), "", "确定删除该订单吗？") {
                viewModel.deleteComplaint(it)
            }
        })

        //跳转待评价页面
        viewModel.navScoreEvent.observe(this, {
            startContainerActivity(AppConstants.Router.Report.F_SERVICE_SCORE,
                Bundle().apply {
                    putString(AppConstants.BundleKey.COMPLAINT_OREDER_NO, it)
                    putInt(AppConstants.BundleKey.COMMENT_TYPE_KEY,AppConstants.Constants.COMMENT_TYPE_COMPLAINT)
                })
        })
        LiveBusCenter.observeCancelComplaintEvent(this,{
            back()
        })
        LiveBusCenter.observeDeleteComplaintEvent(this,{
            back()
        })

        LiveBusCenter.observeScoreCompleteEvent(this,{
            viewModel.getData(viewModel.orderNo.get().toString())
        })

    }
    private fun callPhone(phoneNum: String) {
        val intent = Intent(Intent.ACTION_DIAL)
        val data: Uri = Uri.parse("tel:$phoneNum")
        intent.data = data
        startActivity(intent)
    }
    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }
}